package vincent.com.layouttest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;

import vincent.com.layouttest.util.LogUtil;
import vincent.com.layouttest.view.SlidingPiece;

/**
 * Created by chenzhengwen on 2015/11/9.
 */
public class DrawerDemoActivity extends ColorStatusActivity{
    private static final String TAG = "DrawerDemoActivity";
    private SlidingPiece slidingPiece;
    private FrameLayout iconsContainer;
    private int mOriginalMargin = 0;
    private int mImgSize = 0;
    private int[] mDistanceArray;
    private int mTotalItems = 0;
    private int[] mDeltaMovementDistance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_limitation_uc);

        slidingPiece = (SlidingPiece)findViewById(R.id.drawer);
        iconsContainer = (FrameLayout)findViewById(R.id.icons);

        mTotalItems = 5;

        final int pairsCount = (int)Math.floor(mTotalItems/2);
        mDistanceArray = new int[pairsCount];
        mDeltaMovementDistance = new int[pairsCount];

        slidingPiece.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int viewW = slidingPiece.getMeasuredWidth();

                int marginL, marginT, marginR, marginB;
                marginL = marginT = marginR = marginB = 0;
                int imgSize = mImgSize = (int) (20 * getResources().getDisplayMetrics().density);
                int marginTopOrBottomOffset = (int) (10 * getResources().getDisplayMetrics().density);
                int marginLeftOrRightOffset = mOriginalMargin = (viewW - imgSize * mTotalItems) / (mTotalItems + 1);

                //add bottom icons
                for (int i = 0; i < pairsCount; i++) {
                    mDistanceArray[i] = viewW / 2 - imgSize * (i + 1) - marginLeftOrRightOffset * (i + 1) + imgSize / 2;
                    mDeltaMovementDistance[i] = (viewW - imgSize * (mTotalItems - i * 2)) / 2 - marginLeftOrRightOffset * (i + 1) - imgSize * i;
                    Log.i("delta ", mDeltaMovementDistance[i] + "");
                }

                ImageView imgV = null;
                FrameLayout.LayoutParams params = null;
                for (int i = 0; i < mTotalItems; i++) {
                    imgV = new ImageView(DrawerDemoActivity.this);
                    imgV.setImageDrawable(getResources().getDrawable(R.drawable.fan));
                    params = new FrameLayout.LayoutParams(imgSize, imgSize);

                    marginL = marginLeftOrRightOffset * (i + 1) + imgSize * i;
                    marginT = marginTopOrBottomOffset;
                    marginR = 0;
                    marginB = marginTopOrBottomOffset;
                    params.setMargins(marginL, marginT, marginR, marginB);
                    imgV.setLayoutParams(params);
                    iconsContainer.addView(imgV);
                }

                slidingPiece.setDrawerBottom(marginTopOrBottomOffset * 2 + imgSize);
                slidingPiece.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });

        slidingPiece.setSlidingListener(new SlidingPiece.OnSlidingListener() {
            @Override
            public void onSlidingStart(int slidingHeight, int totalHeight) {
            }

            @Override
            public void onSliding(int slidingHeight, int totalHeight) {
                LogUtil.i(LogUtil.DEBUG_TAG, "onSliding slidingHeight=" + slidingHeight + " | totalHeight=" + totalHeight);
                moveItem(0, mTotalItems, slidingHeight, totalHeight, mDistanceArray, true); //1

                moveItem(1, mTotalItems, slidingHeight, totalHeight, mDistanceArray, true); //2

                moveItem(2, mTotalItems, slidingHeight, totalHeight, mDistanceArray, true); //3

                moveItem(3, mTotalItems, slidingHeight, totalHeight, mDistanceArray, true); //4

                moveItem(4, mTotalItems, slidingHeight, totalHeight, mDistanceArray, false); //5

            }

            @Override
            public void onSlidingEnd(int slidingHeight, int totalHeight) {
                LogUtil.i(LogUtil.DEBUG_TAG, "onSlidingEnd slidingHeight=" + slidingHeight + " | totalHeight=" + totalHeight);
                moveItem(0, mTotalItems, slidingHeight, totalHeight, mDistanceArray, true); //1

                moveItem(1, mTotalItems, slidingHeight, totalHeight, mDistanceArray, true); //2

                moveItem(2, mTotalItems, slidingHeight, totalHeight, mDistanceArray, true); //3

                moveItem(3, mTotalItems, slidingHeight, totalHeight, mDistanceArray, true); //4

                moveItem(4, mTotalItems, slidingHeight, totalHeight, mDistanceArray, false); //5

            }
        });
    }

    @Override
    public View onCreateView(ViewGroup rootView) {
        return LayoutInflater.from(this).inflate(R.layout.activity_limitation_uc, rootView, true);
    }

    @Override
    public String getToolbarTitle() {
        return "Drawer Demo";
    }

    //move the view
    public void moveItem(int index, int totalItems, int slidingHeight, int totalHeight, int[] distances, boolean showTransition){
        ImageView imgV = null;
        FrameLayout.LayoutParams params = null;

        imgV = (ImageView) iconsContainer.getChildAt(index);
        params = (FrameLayout.LayoutParams) imgV.getLayoutParams();

        int left, top, right, bottom;

        left = mOriginalMargin * (index + 1) + mImgSize * index;
        top = params.topMargin;
        right = 0;
        bottom = params.bottomMargin;

        int[] flags = new int[distances.length];
        for(int i = 0; i < distances.length; i++){
            flags[i] = (int) ((float)slidingHeight / (float)totalHeight * ((float)distances[i]));
        }

        float alphaFlag = 1.0f;
        float movement = 0f;

        if(totalItems % 2 == 1 && index == (totalItems - 1)/2){
            left = mOriginalMargin * (index + 1) + mImgSize * index;

            if(slidingHeight == 0){
                movement = mOriginalMargin;
            }else if(slidingHeight == totalHeight){
                movement = 0f;
            }else{
                ImageView imgV2 = (ImageView)iconsContainer.getChildAt(index - 1);

                if(imgV2 != null){
                    movement = left - imgV2.getRight();
                }
            }

            alphaFlag = (float)movement/(float)mOriginalMargin * 1.0f;
        }else if(index % 2 == 0){
            if(index > (totalItems - 1)/2){
                left -= flags[totalItems - index - 1];

                movement = Math.abs(left - mOriginalMargin * (index + 1) - mImgSize * index);
                alphaFlag = 1.0f - (float)movement/(float) mDeltaMovementDistance[totalItems - index - 1] * 1.0f;
            }else{
                left += flags[index];

                movement = left - mOriginalMargin * (index + 1) - mImgSize * index;
                alphaFlag = 1.0f - (float)movement/(float) mDeltaMovementDistance[index] * 1.0f;
            }
        }else{
            if(index > (totalItems - 1)/2){
                left -= flags[totalItems - index - 1];

                movement = Math.abs(left - mOriginalMargin * (index + 1) - mImgSize * index);
                alphaFlag = 1.0f - (float)movement/(float) mDeltaMovementDistance[totalItems - index - 1] * 1.0f;
            }else{
                left += flags[index];

                movement = left - mOriginalMargin * (index + 1) - mImgSize * index;
                alphaFlag = 1.0f - (float)movement/(float) mDeltaMovementDistance[index] * 1.0f;
            }
        }

        if(alphaFlag < 0){
            alphaFlag = 0.0f;
        }

        params.setMargins(left, top, right, bottom);
        imgV.setLayoutParams(params);
        if(showTransition) {
            imgV.setAlpha(alphaFlag);
        }
    }

}
